// 布局信息
// import { hasIn } from 'lodash'
import * as types from '@/store/mutation-types'
import api from '@/tools/api.js'
import promise from '@/tools/promise'
import { SUCCESS_STATUS } from '@/config/status'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  goodsIncreTendency: {},
  goodsList: [],
  pic: {},
  goodsInfo: {},
  fileList: [],
  attrsInfo: [],
  saveData: {}
}

// mutations
const mutations = {
  [types.GOODS_INCRE_TENDENCY] (state, payload) {
    state.goodsIncreTendency = payload
  },
  [types.GOODS_LIST] (state, payload) {
    state.goodsList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.GOODS_PIC] (state, data) {
    state.pic = data
  },
  [types.GOODS_SAVE_DATA] (state, data) {
    state.saveData = data
  },
  [types.GOODS_INFO] (state, res) {
    state.goodsInfo = res
    let fileIds = res.imgs_ids
    let fileUrls = res.imgs_urls
    let fileList = []
    fileIds = fileIds.split(',')
    fileUrls = fileUrls.split(',')
    for (let i = 0; i < Number(fileIds.length); i++) {
      fileList.push({name: fileIds[i], url: fileUrls[i]})
    }
    state.fileList = fileList
    let attrsInfo = []
    let attrsArray = []
    let attrsIndex = []
    let attrsList = []
    let compare = function (prop) {
      return function (obj1, obj2) {
        let val1 = obj1[prop]
        let val2 = obj2[prop]
        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
          val1 = Number(val1)
          val2 = Number(val2)
        }
        if (val1 < val2) {
          return -1
        } else if (val1 > val2) {
          return 1
        } else {
          return 0
        }
      }
    }
    for (let Key in res.goods_attrs) {
      attrsArray.push(res.goods_attrs[Key])
    }
    for (let i = 0; i < Number(attrsArray.length); i++) {
      if (Array.isArray(attrsArray[i]) !== true) {
        attrsInfo[i] = {en_name: attrsArray[i].en_name, value: String(attrsArray[i].value)}
      }
    }
    for (let i = 0; i < Number(attrsArray.length); i++) {
      if (Array.isArray(attrsArray[i]) !== true) {
        attrsIndex.push({index: i, id: attrsArray[i].id})
      }
    }
    attrsIndex = attrsIndex.sort(compare('id'))
    for (let i = 0; i < Number(attrsIndex.length); i++) {
      attrsList.push(attrsInfo[Number(attrsIndex[i].index)])
    }
    state.attrsInfo = attrsList
  }
}

// actions
const actions = {
  // servergoods/goods/Goods/goodsIncreTendency
  async goodsIncreTendency ({ commit }) {
    let getRes = await api('servergoods/goods/Goods/goodsIncreTendency', {})
    SUCCESS_STATUS && commit(types.GOODS_INCRE_TENDENCY, getRes.res)
    return promise(getRes)
  },
  // 获取商品列表
  async goodsList ({ commit }, payload) {
    let listRes = await api('servergoods/goods/Goods/adminGoodsList', payload)
    commit(types.GOODS_LIST, listRes.res)
  },
  // 商品下架
  async goodsOff ({ commit }, payload) {
    let offRes = await api('servergoods/goods/Goods/soldOutOneGoods', payload)
    return promise(offRes)
  },
  // 商品删除
  async goodsDel ({ commit }, payload) {
    let delRes = await api('servergoods/goods/Goods/manageDelGoods', payload)
    return promise(delRes)
  },
  // 商品审核
  async goodsStatus ({ commit }, payload) {
    let statusRes = await api('servergoods/goods/Goods/auditOneGoods', payload)
    return promise(statusRes)
  },
  // 商品添加
  async goodsAdd ({ commit }, payload) {
    let addRes = await api('servergoods/goods/Goods/yihuoAddGoods', payload)
    return promise(addRes)
  },
  // 获取商品详情
  async goodsInfo ({ commit }, payload) {
    let infoRes = await api('servergoods/goods/Goods/goodsAllDetail', payload)
    commit(types.GOODS_INFO, infoRes.res)
  },
  // 获取商品编辑
  async goodsEdit ({ commit }, payload) {
    let editRes = await api('servergoods/goods/Goods/yihuoEditGoods', payload)
    return promise(editRes)
  },
  // 商品价格批量修改
  async batchChangePrice ({ commit }, payload) {
    let batchRes = await api('servergoods/goods/Goods/batchChangePrice', payload)
    return promise(batchRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
